package cms.simmytech.util;

import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Comments :
 * @Project :
 * @author wangyp2
 * @date 2012-2-2
 * @version 1.0
 */
public class StringUtils {

	/**
	 * @author wangyp2
	 * @date 2012-2-2
	 */
	private static String fileName = "config_online";

	public static String returnNull(String string) {
		if (string == null || string.equals(null)) {
			return "null";
		} else {
			return string;
		}
	}

	public static String returnEmpty(String string) {
		if (string == null || string.equals(null)) {
			return "";
		} else {
			return string;
		}
	}

	public static String cut86(String string) {
		if (string.indexOf("+86") >= 0) {
			return string.replace("+86", "");
		} else {
			return string;
		}
	}

	public static String cutHyphen(String string) {
		if (string.indexOf("-") >= 0) {
			return string.replace("-", "");
		} else {
			return string;
		}
	}

	public static boolean isEmpty(String string) {
		return org.apache.commons.lang3.StringUtils.isEmpty(string);
	}

	public static String[] cutKeyValue(String string) {
		String[] strings = null;
		if (string.indexOf("=") >= 0) {
			strings = string.split("=");
		}
		return strings;
	}

	public static String cutValue(String string) {
		String valueString = null;
		if (string.indexOf("=") >= 0) {
			valueString = string.split("=")[1];
		}
		return valueString;
	}

	public static boolean checkEmail(String email) {
		Pattern pattern = Pattern
		        .compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
		Matcher matcher = pattern.matcher(email);
		return matcher.find();
	}

	public static String returnFullUrl(String url, String website) {
		if (StringUtils.isEmpty(url)) {
			return "";
		}
		if (url.contains("http://") || url.contains("https://")) {
			return url;
		} else {
			return website + url;
		}
	}

	// public static String replaceLocalUrl(String url) {
	// if (!url.contains(PropertyUtil.getProper("HTTP_LINK",
	// Constant.CONFIG_FILE))) {
	// return url;
	// } else {
	// return url.replace(PropertyUtil.getProper("HTTP_LINK",
	// Constant.CONFIG_FILE), "");
	// }
	// }

	public static String convert(String utfString) {
		StringBuilder sb = new StringBuilder();
		int i = -1;
		int pos = 0;

		while ((i = utfString.indexOf("\\u", pos)) != -1) {
			sb.append(utfString.substring(pos, i));
			if (i + 5 < utfString.length()) {
				pos = i + 6;
				sb.append((char) Integer.parseInt(utfString.substring(i + 2, i + 6), 16));
			}
		}

		return sb.toString();
	}

	public static String formatThousand(String numberString) {
		Pattern p = Pattern.compile("(?<=\\d)(?=(\\d\\d\\d)+$)");
		Matcher m = p.matcher(numberString);
		return m.replaceAll(",");
	}

	public static String formatThousand(BigDecimal number) {
		String numberString = number.intValue() + "";
		Pattern p = Pattern.compile("(?<=\\d)(?=(\\d\\d\\d)+$)");
		Matcher m = p.matcher(numberString);
		return m.replaceAll(",");
	}

	public static boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}

	public static String delHTMLTag(String htmlStr) {
		String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
		String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
		String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式

		Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
		Matcher m_script = p_script.matcher(htmlStr);
		htmlStr = m_script.replaceAll(""); // 过滤script标签

		Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
		Matcher m_style = p_style.matcher(htmlStr);
		htmlStr = m_style.replaceAll(""); // 过滤style标签

		Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
		Matcher m_html = p_html.matcher(htmlStr);
		htmlStr = m_html.replaceAll(""); // 过滤html标签
		return htmlStr.trim(); // 返回文本字符串
	}

	public static String toChineseNumber(String string) {
		String[] s1 = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
		String[] s2 = { "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千" };

		String result = "";

		int n = string.length();
		for (int i = 0; i < n; i++) {
			int num = string.charAt(i) - '0';
			if (i != n - 1 && num != 0) {
				result += s1[num] + s2[n - 2 - i];
			} else {
				result += s1[num];
			}
		}
		// 去掉一十的一
		if (result.indexOf("一十") != -1) {
			result = result.replaceAll("一十", "十");
		}
		int length = result.length();
		for (int i = 0; i < length; i++) {
			if (result.lastIndexOf("零") == result.length() - 1) {
				result = result.substring(0, result.length() - 1);
			} else {
				break;
			}

		}
		return result;

	}

	/**
	 * 根据指定长度在数字前加零
	 * 
	 * @param outLength
	 *            输出长度
	 * @param number
	 *            数字值
	 * @return
	 */
	public static String numberAddZeroToString(int outLength, int number) {
		String returnString = "";
		if (number > 0) {
			try {
				String numberString = number + "";
				int numberLength = numberString.length();
				if (numberLength < outLength) {
					StringBuilder stringBuilder = new StringBuilder();
					for (int i = 0; i < outLength - numberLength; i++) {
						stringBuilder.append(0);
					}
					stringBuilder.append(numberString);
					returnString = stringBuilder.toString();
				} else {
					returnString = numberString;
				}
			}
			catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		return returnString;
	}

	public static boolean isNumner(String str) {
		Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
		return pattern.matcher(str).matches();
	}

	public static String increaseUserNameNumber(String numberName, String name) {
		String newUserName = null;
		String lastLetter = numberName.replace(name, "");
		if (StringUtils.isEmpty(lastLetter)) {
			newUserName = name + "1";
		} else {
			if (isNumner(lastLetter)) {
				newUserName = name + (Integer.valueOf(lastLetter) + 1);
			}
		}
		return newUserName;
	}

	/**
	 * main函数.
	 * 
	 * @param args
	 *            启动参数
	 * @throws Exception
	 *             Exception
	 */
	public static void main(String... args) throws Exception {
		// for (int i = 0; i < 100; i++) {
		// System.out.println(toChineseNumber(i + ""));
		// }
		// System.out.println(numberAddZeroToString(2, 222));
		System.out.println(increaseUserNameNumber("关羽4324", "关羽"));
	}
}
